<?php
namespace App\Listeners;

use App\Events\In\NormalBuyingFinished;
use App\Events\In\StudentAppointmentStatusChanged;
use App\Facades\KafkaBridge;
use App\Models\LuckyBag;
use Carbon\Carbon;

class OnLuckyBagStudentAppointmentStatusChanged
{
    public function handle(StudentAppointmentStatusChanged $event)
    {
        $studentId = $event->studentId;
        if (intval($event->toStatus) !== 3) return;

        //上完课创建福袋
        $appointmentId = $event->appointmentId;
        $luckBag = LuckyBag::where('appointment_id', $appointmentId)->first();
        if (empty($luckBag)) {
            $luckBag = new LuckyBag();
            $luckBag->setStudentId($studentId);
            $luckBag->setAppointmentId($appointmentId);
            $luckBag->setShareCode(uniqid(LuckyBag::SHARE_CODE_PREFIX));
            $luckBag->save();
        }
    }
}